package org.dtrd.modules.evaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DiseaseEvaluationInfo;
import org.dtrd.modules.evaluation.entity.dto.DiseaseDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdNeuropathy;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNeuropathyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author: QR
 * @Date: 2021/9/27-18:02
 */
@Api(tags = "量表评估")
@RestController
public class NeuropathyController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdNeuropathyService neuropathyService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加神经病变评估量表记录")
    @PostMapping("/105/22/1052101")
    public Result<?> saveNeuropathyInfo(@RequestBody DiseaseDTO dto){
        // 参数校验
        if (dto.getIsDiagnose() == null || dto.getSymptom() == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (dto.getIsDiagnose() <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        // 校验是否有空值, vue 传入数字列表为空时值默认为 -1
        Optional<Integer> any = Arrays.stream(dto.getSymptom())
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()){
            return error(STATE_PARAMS_MISSING);
        }
        DiseaseEvaluationInfo info = neuropathyService.saveNeuropathyInfo(dto);
        String logContent = String.format("添加神经病变评估量表记录，添加用户id：%d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("获取最近一次的神经病变量表评估记录-通过patientId")
    @GetMapping("/105/22/1052102")
    public Result<?> getNeuropathyInfoLatest(@RequestParam("patientId") Integer patientId){
        if (patientId == null) {
            return Result.error(STATE_PARAMS_MISSING.getCode(), STATE_PARAMS_MISSING.getMsg());
        }
        if (patientId <= 0) {
            return Result.error(STATE_PARAMS_ERROR.getCode(), STATE_PARAMS_ERROR.getMsg());
        }
        DiseaseEvaluationInfo diseaseEvaluationInfo = neuropathyService.getNeuropathyInfoLatest(patientId);
        String logContent = String.format("获取最近一次的神经病变量表评估记录，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(diseaseEvaluationInfo);
    }

    @ApiOperation("获取神经病变量评估记录列表")
    @GetMapping("/105/22/1052103")
    public Result<?> getNeuropathyInfoList(@RequestParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)){
            return error(STATE_PARAMS_MISSING);
        }
        LambdaQueryWrapper<DtrdEntEvaluationRdNeuropathy> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdNeuropathy.class)
                .eq(DtrdEntEvaluationRdNeuropathy::getPatientId, patientId);
        List<DtrdEntEvaluationRdNeuropathy> poList = neuropathyService.list(queryWrapper);
        List<DiseaseEvaluationInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()){
            dtoList = poList.stream()
                    .map(po -> new DiseaseEvaluationInfo().parseFromPo(po))
                    .collect(Collectors.toList());
        }
        String logContent = String.format("获取神经病变量评估记录列表，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(dtoList);
    }

    @ApiOperation("删除一条神经病变量表评估记录")
    @DeleteMapping("/105/22/1052104")
    public Result<?> removeNeuropathyInfo(@RequestParam("dataId") Integer dataId){
        if (isIntParamInvalid(dataId)){
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = neuropathyService.removeById(dataId);
        String logContent = String.format("删除一条神经病变量表评估记，删除记录id：%d", dataId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
